package org.dut.community.controller;

import org.dut.community.service.DataService;
import org.dut.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class DataController {
    @Autowired
    private DataService dataService;

    @RequestMapping(path = "/data",method = {RequestMethod.GET,RequestMethod.POST})
    public String getDataPage(){
        return "/site/admin/data";
    }

    @ResponseBody
    @RequestMapping(path = "/data/uv",method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
        if(start == null || end == null || start.after(end)) {
            return CommunityUtil.getJSONString(1,"参数为空 或 结束日期早于开始日期");
        }
        long uv = dataService.calculateUV(start, end);
        List<Long> uvList = dataService.getUVChartDate(start, end);
        HashMap<String,Object> map = new HashMap<>();
        map.put("uvData",uvList);
        map.put("uvResult",uv);
        return CommunityUtil.getJSONString(0,"success",map);
    }

    @ResponseBody
    @RequestMapping(path = "/data/dau",method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
        if(start == null || end == null || start.after(end)) {
            return CommunityUtil.getJSONString (1,"参数为空 或 结束日期早于开始日期");
        }
        long dau = dataService.calculateDAU(start, end);
       List<Long> dauList = dataService.getDAUChartDate(start, end);
       HashMap<String,Object> map = new HashMap<>();
        map.put("dauData",dauList);
        map.put("dauResult",dau);
        return CommunityUtil.getJSONString(0,"success",map);
    }
}
